home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / appsrcs.zip / APPPRINT.ZIP / SELECT2.C < prev   
C/C++ Source or Header  |  1993-03-23  |  8KB  |  300 lines

  1. From 71553.3104@compuserve.com Sun Mar 14 16:26:28 1993
  2. Return-Path: <71553.3104@compuserve.com>
  3. Received: from ph.tn.tudelft.nl by duttfks.tn.tudelft.nl (4.1/HB-1.13)
  4.     id AA08366; Sun, 14 Mar 93 16:26:26 +0100
  5. Received: from ihb.compuserve.com by ph.tn.tudelft.nl (4.1/HB-1.18)
  6.     id AA07995; Sun, 14 Mar 93 16:23:10 +0100
  7. Received: by ihb.compuserve.com (5.65/5.930129sam)
  8.     id AA04091; Sun, 14 Mar 93 10:23:33 -0500
  9. Date: 14 Mar 93 10:18:37 EST
  10. From: Frits Wiarda <71553.3104@CompuServe.COM>
  11. To: Geert van Kempen <geert@ph.tn.tudelft.nl>
  12. Subject: Printer setup example
  13. Message-Id: <930314151837_71553.3104_DHS19-1@CompuServe.COM>
  14. Status: OR
  15.  
  16. static char GlobalPrinterName[STRING_SIZE];
  17.  
  18.  
  19. BOOL CALLBACK SelectPrinterDialogProc(HWND hwnd , UINT message, WPARAM 
  20. wParam, 
  21.     LPARAM lPAram);
  22.     
  23.  
  24. int SelectPrinterDialog(HINSTANCE hInstance, HWND hWndParent,
  25.     char PrinterName[])
  26. {
  27.     DLGPROC lpfnDialogProc;
  28.     int ReturnValue;
  29.  
  30.     /* Display dialog box. */
  31.     lpfnDialogProc=(DLGPROC)MakeProcInstance(
  32.         (FARPROC)SelectPrinterDialogProc,hInstance);
  33.     ReturnValue=DialogBox(hInstance,"SelectPrinterDialog",hWndParent,
  34.         lpfnDialogProc);
  35.     FreeProcInstance((FARPROC)lpfnDialogProc);
  36.     
  37.     /* If dialog has successfully ended, copy the printer name form
  38.     the static global variable to the parameter variable. */
  39.     if(ReturnValue==0)
  40.     {
  41.         strncpy(PrinterName,GlobalPrinterName,STRING_SIZE-1);
  42.         PrinterName[STRING_SIZE-1]='\0';
  43.     }
  44.  
  45.     /* return. On succes the return value is 0. */
  46.     return ReturnValue;
  47. }
  48.  
  49.  
  50.  
  51.  
  52. BOOL CALLBACK SelectPrinterDialogProc(HWND hwnd,UINT message,WPARAM wParam, 
  53.     LPARAM lParam)
  54. {
  55.     char Message[STRING_SIZE];
  56.     static char AllPrinterNames[ALL_PRINT_NAME_SIZE];
  57.     static char *PrinterNameTable[PRINT_NAME_TAB_SIZE];
  58.     char PrinterDescription[STRING_SIZE];
  59.     char *PrinterName,*PrinterDriver,*PrinterPort;
  60.     char ListEntry[STRING_SIZE];
  61.     char DriverFile[STRING_SIZE];
  62.     int i,j;
  63.  
  64.     LRESULT CurSel;
  65.     HINSTANCE hLibrary;
  66.     typedef void (WINAPI *DRIVERPROC)(HWND hwnd, HINSTANCE hLibrary,
  67.         LPSTR szPrinterName, LPSTR szPrinterPort);
  68.     DRIVERPROC lpfnDriverProc;
  69.     
  70.     switch(message)
  71.     {
  72.  
  73.         /* Initialisation. */
  74.         case WM_INITDIALOG:
  75.         
  76.             /* Get list containing the names of all available printers. */
  77.             GetProfileString("devices",0,"",AllPrinterNames,
  78.                 ALL_PRINT_NAME_SIZE-1);
  79.                 
  80.             /* Fill the table AllPrinterNames with the names of the
  81.             printers. AllPrinterNames will be terminated with a NULL. */
  82.             for(i=0,j=0;AllPrinterNames[j]!='\0' && 
  83.                 i<PRINT_NAME_TAB_SIZE;i++,j++)
  84.             {
  85.             PrinterNameTable[i]=AllPrinterNames+j;
  86.             while(AllPrinterNames[j]!='\0')j++;
  87.             }
  88.             PrinterNameTable[i]=NULL;
  89.             
  90.             
  91.         for(i=0;PrinterNameTable[i]!=NULL;i++)
  92.         {
  93.             /* Get the port name of each printer. If it
  94.             is not possiple to get the port name of the
  95.             printer, remove the printer from the list. */
  96.             GetProfileString("devices",PrinterNameTable[i],",,",
  97.                 PrinterDescription,STRING_SIZE);
  98.             if( (PrinterDriver=strtok(PrinterDescription,", "))
  99.                 ==NULL || (PrinterPort=strtok(NULL,", "))
  100.                 ==NULL )
  101.             {
  102.                 for(j=i;PrinterNameTable[j+1]!=NULL;
  103.                     j++)PrinterNameTable[j]=
  104.                     PrinterNameTable[j+1];
  105.                 PrinterNameTable[j]=NULL;
  106.                 i--;
  107.                 continue;
  108.             }
  109.  
  110.             /* The complete text for a printer in the list
  111.             box is "PrinterName on PrinterPort". */
  112.             strncpy(ListEntry,PrinterNameTable[i],STRING_SIZE-1);
  113.             ListEntry[STRING_SIZE-1]='\0';
  114.             strncat(ListEntry," on ",STRING_SIZE-    
  115.                 strlen(ListEntry)-1);
  116.             strncat(ListEntry,PrinterPort,STRING_SIZE-    
  117.                 strlen(ListEntry)-1);
  118.  
  119.             /* If the printer port is "none", remove the printer
  120.             from the list. */
  121.             for(j=0;PrinterPort[j]!='\0';j++)if
  122.                 ('A'<=PrinterPort[j] &&
  123.                 PrinterPort[j]<='Z')
  124.                 PrinterPort[j]+='a'-'A';
  125.             if(strcmp(PrinterPort,"none")==0)
  126.             {
  127.                 for(j=i;PrinterNameTable[j+1]!=NULL;
  128.                     j++)PrinterNameTable[j]=
  129.                     PrinterNameTable[j+1];
  130.                 PrinterNameTable[j]=NULL;
  131.                 i--;
  132.                 continue;
  133.             }
  134.             
  135.             /* Add printer to the list box. */
  136.              SendDlgItemMessage(hwnd,IDD_LISTBOX_PRINTERS,
  137.                 LB_INSERTSTRING,i,
  138.                 (LONG)(char far *)ListEntry);
  139.         }
  140.  
  141.         /* Get the name of the default printer. */
  142.         GetProfileString("windows","device",",,,",PrinterDescription,
  143.             STRING_SIZE);
  144.         if( (PrinterName=strtok(PrinterDescription,","))==NULL )
  145.         {
  146.             SendDlgItemMessage(hwnd,IDD_LISTBOX_PRINTERS,
  147.                 LB_SETCURSEL,0,0L);
  148.                     return TRUE;
  149.         }
  150.         
  151.         /* Find the default printer in the list of printer. If
  152.         found, select this printer in the list box.*/
  153.         for(i=0;PrinterNameTable[i]!=NULL;i++)
  154.         {
  155.             if(strcmp(PrinterNameTable[i],PrinterName)==0)
  156.             {
  157.                 
  158. SendDlgItemMessage(hwnd,IDD_LISTBOX_PRINTERS,
  159.                 LB_SETCURSEL,i,0L);
  160.                 return TRUE;
  161.             }
  162.         }
  163.  
  164.         /* If there is no default printer, the first printer in
  165.         the list is selected. */
  166.         
  167. SendDlgItemMessage(hwnd,IDD_LISTBOX_PRINTERS,LB_SETCURSEL,
  168.             0,0L);
  169.  
  170.             return TRUE;
  171.  
  172.         case WM_COMMAND:
  173.  
  174.         switch(wParam)
  175.         {
  176.  
  177.             /* The OK button is pressed. */
  178.             case IDD_BUTTON_OK:
  179.             
  180.                 /* Get the ordinal number of the currently selected
  181.                 printer. */
  182.             
  183. CurSel=SendDlgItemMessage(hwnd,IDD_LISTBOX_PRINTERS,
  184.                 LB_GETCURSEL,0,0L);
  185.                 if(CurSel==LB_ERR)return TRUE;
  186.                 
  187.                 /* Copy the name of the currently selected printer
  188.                 into a global variable. */
  189.                 strncpy(GlobalPrinterName,PrinterNameTable[CurSel],
  190.                 STRING_SIZE-1);
  191.             GlobalPrinterName[STRING_SIZE-1]='\0';
  192.                 
  193.                 /* terminate the dialog. */
  194.                 EndDialog(hwnd,0);
  195.                 
  196.             return TRUE;
  197.  
  198.             /* The cancel button is pressed, or the close option
  199.             from the system menu is selected. */
  200.             case IDD_BUTTON_CANCEL:
  201.             case IDCANCEL:
  202.  
  203.             /* Terminate the dialog. */
  204.             EndDialog(hwnd,1);
  205.             
  206.             return TRUE;
  207.  
  208.             /* The setup button is pressed. */
  209.             case IDD_BUTTON_SETUP:
  210.  
  211.                 /* Get the ordinal number of the currently selected
  212.                 printer. */
  213.                 CurSel=SendDlgItemMessage(hwnd,
  214.                     IDD_LISTBOX_PRINTERS,LB_GETCURSEL,0,0);
  215.                 if(CurSel==LB_ERR)return TRUE;
  216.  
  217.             /* Get the driver file of the selected printer,
  218.             and the port it is connected to. */
  219.             GetProfileString("devices",PrinterNameTable[CurSel],",,",
  220.                 PrinterDescription,STRING_SIZE);
  221.             if( (PrinterDriver=strtok(PrinterDescription,", "))
  222.                 ==NULL || (PrinterPort=strtok(NULL,", "))
  223.                 ==NULL )
  224.             {
  225.                 sprintf(Message,
  226.                     "%s\This printer is not properly 
  227. installed.",
  228.                     PrinterNameTable[CurSel]);
  229.                 MessageBox(hwnd,Message,"Printer Setup",
  230.                     MB_ICONEXCLAMATION|MB_OK);
  231.             }
  232.             strncpy(DriverFile,PrinterDriver,STRING_SIZE-1);
  233.             DriverFile[STRING_SIZE-1]='\0';
  234.             strncat(DriverFile,".drv",STRING_SIZE-
  235.                 strlen(DriverFile)-1);
  236.  
  237.             /* Start the DEVICEMODE procedure from the
  238.             driver file. */
  239.             hLibrary=LoadLibrary(DriverFile);
  240.             if((unsigned int)hLibrary<32)
  241.             {
  242.                 sprintf(Message,
  243.                     "%s\nThis driver file is invalid.",
  244.                     DriverFile);
  245.                 MessageBox(hwnd,Message,"Printer Setup",
  246.                     MB_OK|MB_ICONEXCLAMATION);
  247.             }
  248.             else
  249.             {
  250.                 lpfnDriverProc=(DRIVERPROC)
  251.                     
  252. GetProcAddress(hLibrary,"DEVICEMODE");
  253.                 (*lpfnDriverProc)(hwnd,hLibrary,
  254.                     (LPSTR)PrinterNameTable[CurSel],
  255.                     (LPSTR)PrinterPort);
  256.                 FreeLibrary(hLibrary);
  257.             }
  258.             
  259.             return TRUE;
  260.             
  261.                case IDD_LISTBOX_PRINTERS:
  262.  
  263.             switch(HIWORD(lParam))
  264.             {
  265.             
  266.                 /* A printer in the select box is double 
  267.                 clicked on. */
  268.                 case LBN_DBLCLK:
  269.                 
  270.                     /* Get the ordinal number of the currently 
  271.                     selected printer. */
  272.                     CurSel=SendDlgItemMessage(hwnd,
  273.                         
  274. IDD_LISTBOX_PRINTERS,LB_GETCURSEL,0,0);
  275.                     if(CurSel==LB_ERR)return TRUE;
  276.                     
  277.                     /* Copy the name of the selected printer
  278.                     to a global variable. */
  279.                     strncpy(GlobalPrinterName,
  280.                         PrinterNameTable[CurSel],
  281.                         STRING_SIZE-1);
  282.                 GlobalPrinterName[STRING_SIZE-1]='\0';
  283.                         
  284.                     /* Terminate the dialog. */
  285.                 EndDialog(hwnd,0);
  286.                     return TRUE;
  287.                     
  288.             }
  289.             break;
  290.         }
  291.         break;
  292.     }
  293.     return FALSE;
  294.  
  295. }
  296.        
  297.  
  298.  
  299.  
  300.